package com.devshephard.juchm.threadPool;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * invokeAny,返回一个最先得到结果的任务
 */
public class TestInvokeAny {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //当线程池只有一个的时候,只会返回最先排入线程池核心执行的任务
        ExecutorService pool = Executors.newFixedThreadPool(1);

        String str = pool.invokeAny(Arrays.asList(
                () -> {
                    TimeUnit.SECONDS.sleep(10);
                    System.out.println("first");
                    return "1";
                },
                () -> {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("second");
                    return "2";
                }
        ));

        System.out.println(str);
        System.out.println("main");

    }
    
}
